﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net; 
using UnityEngine.UI;

public class Config {
    /// <summary>
    /// Socket数据结构
    /// </summary>
    public static DataModen _DataModen = null;
    /// <summary>
    /// 当前ClientID
    /// </summary>
    public static ClientData _ClientData = null;
    /// <summary>
    /// 视频播放地址
    /// </summary>
    public const string VIDEONAME = "/Video/Video.mp4";
    /// <summary>
    /// 答题初始积分
    /// </summary>
    public static float SCORE = 10;
    /// <summary>
    /// 存放所有玩家信息
    /// </summary>
    public static List<PlayerData> PlayerDatas = new List<PlayerData>();
    /// <summary>
    /// 创建当前玩家信息
    /// </summary>
    public static PlayerData _PlayerData = null;
    /// <summary>
    /// 当前答题的进度下标，答完题则+=1；
    /// </summary>
    public static int  CUL_QUESTING_INDEX=0;
    /// <summary>
    /// 数据库名称
    /// </summary>
    public const string MYSQL_DATEBASE_NAME = "yuhua_district_taxation_bureau";
    /// <summary>
    /// 数据库表单  也就是题库类型
    /// </summary>
    public static string[] MYSQL_TABLES_NAME = { "party_building", "learning_powerful_nation",  "policy", "thought" };
    public static string[] QUEST_TYPE_CH_NAME = {"党建方向","学习强国","政策方向","思想方向" };//题库类型中文名称
    /// <summary>
    /// 题库中的题目
    /// </summary>
    public static QuestData QUESTIONDATAS = null;
    /// <summary>
    /// 题库类型标志位
    /// </summary>
    public static int QUEST_TYPE = 1;
    /// <summary>
    /// 单人还是双人游戏
    /// </summary>
    public static E_SingerOrMultiplayer _SingerOrMultiplayer = E_SingerOrMultiplayer.Singer;
    /// <summary>
    /// 题库类型JSON数据文本名称
    /// </summary>
    public static string QUESTTYPE_JSONFILE = "QusetType.json";
    /// <summary>
    /// 题库数据JSON文件
    /// </summary>
    public static string QUESTSDATAS= "Questdata.json";
    /// <summary>
    /// 配置文件路径
    /// </summary>
    public static string FILES_PATH = Application.streamingAssetsPath;
    /// <summary>
    /// 游戏状态
    /// </summary>
    public static E_GameState e_GameState = E_GameState.Free;
    /// <summary>
 
    /// <summary>
    /// 是否为播放视频模式
    /// </summary>
    public static bool isPlayVideo = false;
    /// <summary>
    /// 加载网络图片或本地图片。。本地需要在路劲前加"file://"
    /// </summary>
    /// <param name="image"></param>
    /// <param name="url"></param>
    /// <param name="img"></param>
    /// <param name="sprite"></param>
    /// <returns></returns>
    public static IEnumerator GetTexture(Image image, string url, Texture2D img = null, Sprite sprite = null)
    {
        if (!url.Contains("http"))
        {
            var temp = "file://" + url;
            url = temp;
        }
        WWW www = new WWW(url);
        yield return www;
        if (www.isDone && www.error == null)
        {
            img = www.texture;
            sprite = Sprite.Create(img, new Rect(0, 0, img.width, img.height), new Vector2(0.5f, 0.5f));
            image.sprite = sprite;

        }
    }
    /// <summary>
    /// 返回一个子物体
    /// </summary>
    /// <param name="parent"></param>
    /// <param name="childName"></param>
    /// <returns></returns>
    public static GameObject GetChild(Transform parent,string childName) 
    {
        for (int i = 0; i < parent.childCount; i++)
        {
            if (parent.GetChild(i).name==childName)
            {
                return parent.GetChild(i).gameObject;
            }
        }
        return null;
    }
}
[Serializable]
public class ClientData
{
    /// <summary>
    /// 客户端序列号
    /// </summary>
    public int _ClientID { get; set; }
    /// <summary>
    /// 获取本机的机械码
    /// </summary>
    public string _MechanicsCode { get; set; }
    public ClientData(int id,string code) 
    {
        _ClientID = id;
        _MechanicsCode = code;
    }
}
[Serializable]
public class DataModen
{  
    /// <summary>
    /// 题库类型编号
    /// </summary>
    public int _QuestType { get; set; }
    /// <summary>
    /// 显示面板名称
    /// </summary>
    public string _PanelName { get; set; }
    /// <summary>
    /// 目标计算机设置是否为单人赛或者多人联机
    /// </summary>
    public E_SingerOrMultiplayer _SingerOrMultiplayer { get; set; }
    /// <summary>
    /// 命令协议编码
    /// </summary>
    public string _CmdCode { get; set; }
    public PlayerData _PlayerData { get; set; }
    /// <summary>
    /// 服务端返回所有在线客户端数据
    /// </summary>
    public List<PlayerData> _Players { get; set; }
    public DataModen(int type,string panelname,E_SingerOrMultiplayer e_Singer,string code, PlayerData data, List<PlayerData>playerdatas) 
    { 
        _QuestType = type;
        _PanelName = panelname;
        _SingerOrMultiplayer = e_Singer;
        _CmdCode = code;
        _PlayerData = data;
        _Players = playerdatas;
    }
}
[Serializable]
public enum E_SingerOrMultiplayer
{
    Singer,//单人
    Multiplayer  //多人联机
}
public class CulQuestionData 
{
    /// <summary>
    /// 当前答题序号
    /// </summary>
    public int _IndexQuestion { get; set; }
    /// <summary>
    /// 当前答题的类型
    /// </summary>
    public E_QuestionType e_QuestionType { get; set; }
    public string _Title { get; set; }
    /// <summary>
    /// 答案选择元素
    /// </summary>
    public List<string> _ChooseAnswersItems { get; set; }
    /// <summary>
    /// 该题的标准答案 可能有多选
    /// </summary>
    public string _AnswersItems { get; set; }
    public CulQuestionData(int culIndex, E_QuestionType type, string title, List<string> choiseItems, string answers)
    {
        _IndexQuestion = culIndex;
        e_QuestionType = type;
        _Title = title;
        _ChooseAnswersItems = choiseItems;
        _AnswersItems = answers;
    }
}
[Serializable]
public class QuestData 
{
    public int _Questtype;
    public List<CulQuestionData> _QuestionDatas;
    public QuestData(int type,List<CulQuestionData> datas) 
    {
        _Questtype = type;
        _QuestionDatas = datas;
    }
}

[Serializable]
public class PlayerItem
{
    public int _id { get; set; }
    /// <summary>
    /// 客户端信息
    /// </summary>
    public string _RemoteEndPoint { get; set; }
    /// <summary>
    /// 客户端的游戏状态
    /// </summary>
    public E_GameState e_GameState { get; set; }
    public PlayerData playerData { get; set; }
    public PlayerItem(int id,string endpoint,E_GameState state,PlayerData data) 
    {
        _id = id;
        _RemoteEndPoint = endpoint;
        e_GameState = state;
        playerData = data;
    }
}
[Serializable]
public class PlayerData
{ 
    public int _id { get; set; }
    public float _Score { get; set; }
    public float _CorrectRate { get; set; } //答题正确率
    public PlayerData(int id ,float score,float rate) 
    {
        _id = id;
        _Score = score;
        _CorrectRate = rate;
    }
}
[Serializable]
public enum E_GameState
{
    Free,  //空闲状态； 可选进入游戏或者继续播放视频，默认是播放视频
    Video,//播放视频中
    GameReady,//游戏准备中
    InGame,//抢答赛游戏中
    OutGame//离开游戏，可能进入成绩页面 
}
[Serializable]
public enum E_QuestionType
{
    Single,//单选
    Multiple,//多选
    Judgment //判断=单选   存在的意义在于题型出现的几率筛选标记
}
[Serializable]
public class QusetType
{
    public int _QuestID { get; set; }
    public string _QuestType { get; set; }
    public string _UrlPath { get; set; }
    public QusetType(int id,string type,string url) 
    {
        _QuestID = id;
        _QuestType = type;
        _UrlPath = url;
    }
}
 